package com.wb;

import com.wb.mapper.AccountMapper;
import com.wb.mapper.LikedMapper;
import com.wb.model.Account;
import com.wb.model.Liked;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author 魏彪
 */
@Slf4j
public class Test {

    public static void main(String[] args) throws IOException {

        // 第一步：读取mybatis-config.xml配置文件
        // InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        // 读源码可替换为
        // InputStream resource = Test.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
        // 通用写法
        InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("mybatis-config.xml");

        // 第二步：构建SqlSessionFactory（框架初始化） == new DefaultSqlSessionFactory 持有 Configuration类的引用
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        // 打开SqlSession
        SqlSession session = sqlSessionFactory.openSession();

        // 获取Mapper接口对象（底层是动态代理）
        LikedMapper likedMapper = session.getMapper(LikedMapper.class);

        Liked liked = likedMapper.selectByPrimaryKey(7);

        log.info("查询结果>> : {}", liked);

        session.commit();

        session.close();
    }
}
